<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/02/01 09:31:59 $)
// $RCSfile: report_populararticles.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$template->assign("calendar", true);

$interface->staffHeader($_SWIFT["language"]["knowledgebase"]." &gt; ".$_SWIFT["language"]["reports"], 4);

$interface->staffNavBar('<a href="index.php?_m=knowledgebase&_a=managecategories" title="'.$_SWIFT["language"]["knowledgebase"].'">'.$_SWIFT["language"]["knowledgebase"].'</a> &raquo; '.'<a href="index.php?_m=knowledgebase&_a=reports&type=populararticles" title="'.$_SWIFT["language"]["reports"].'">'.$_SWIFT["language"]["reports"].'</a> &raquo; '.'<a href="index.php?_m=knowledgebase&_a=reports&type=populararticles" title="'.$_SWIFT["language"]["rpopulararticles"].'">'.$_SWIFT["language"]["rpopulararticles"].'</a>', "", 4);

printInfoBox($infomessage);
printErrorBox($errormessage);

?>
<form name="swiftform" action="index.php" method="POST"><fieldset class="swiftfieldset">
<legend><?=$_SWIFT["language"]["filter"]?></legend>
<table width="100%"  border="0" cellspacing="1" cellpadding="2" class="tborder">
<tr>
<td width="100" class="row1" nowrap><?=$_SWIFT["language"]["filcategory"]?></td>
<td class="row2"><select name="kbcategoryid" class="swiftselect">
<option value="0"<?=iif(empty($_POST["kbcategoryid"]), " selected")?>><?=$_SWIFT["language"]["filnocat"]?></option>
<?
$options = buildKBCategorySelectData(0);
for ($ii=0; $ii<count($options); $ii++)
{
	$val = &$options[$ii];
	?>
	<option value="<?=$val["value"]?>"<?=iif($_POST["kbcategoryid"]==$val["value"], " selected")?>><?=str_replace(" ", "&nbsp;", htmlspecialchars($val["title"]))?></option>
	<?
}
?></select></td>
<td width="100" class="row1" nowrap><?=$_SWIFT["language"]["filgroup"]?></td>
<td class="row2"><select name="tgroupid" class="swiftselect">
<option value="0"<?=iif(empty($_POST["tgroupid"]), " selected")?>><?=$_SWIFT["language"]["filnogroup"]?></option>
<?
$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."templategroups` ORDER BY `title` ASC;");
while ($dbCore->nextRecord())
{
	?>
	<option value="<?=$dbCore->Record["tgroupid"]?>"<?=iif($_POST["tgroupid"]==$dbCore->Record["tgroupid"], " selected")?>><?=htmlspecialchars($dbCore->Record["title"])?></option>
	<?
}

if (empty($_POST["datefrom"]) && empty($_POST["dateto"]))
{
	//$_gdatefrom = date("d/m/Y", mktime(0,0,0,date("m"), 1, date("Y")));
	$_gdatefrom = generateCalendarTimeline(mktime(0,0,0,date("m"), 1, date("Y")));
	//$_gdateto = date("d/m/Y", DATENOW);
	$_gdateto = generateCalendarTimeline(DATENOW);
} else {
	$_gdatefrom = $_POST["datefrom"];
	$_gdateto = $_POST["dateto"];
}

?></select></td>
<td width="100" class="row1" nowrap><?=$_SWIFT["language"]["fildate"]?></td>
<td class="row2"><input type="text" name="datefrom" id="datefrom" size="12" readonly="1" value="<?=iif(!empty($_gdatefrom), $_gdatefrom)?>" class="swifttext" onclick="return showCalendar('datefrom', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" />&nbsp;<img src="<?=$_SWIFT["themepath"]?>calendar.gif" id="datefromtrigger" style="cursor: pointer;" align="absmiddle" onclick="return showCalendar('datefrom', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" /> <?=$_SWIFT["language"]["filto"]?> <input type="text" name="dateto" id="dateto" size="12" readonly="1" value="<?=iif(!empty($_gdateto), $_gdateto)?>" class="swifttext" onclick="return showCalendar('dateto', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" />&nbsp;<img src="<?=$_SWIFT["themepath"]?>calendar.gif" id="datetotrigger" style="cursor: pointer;" align="absmiddle" onclick="return showCalendar('dateto', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" /></td>
<td width="100" class="row2" align="right" nowrap><input type="submit" class="yellowbutton" name="filter" value="<?=$_SWIFT["language"]["filter"]?>"></td>
</tr>
</table>
</fieldset><BR />
<input type="hidden" name="_m" value="knowledgebase"><input type="hidden" name="_a" value="reports"><input type="hidden" name="type" value="populararticles">
</form>
<?
$_wheresql = array();
if (!empty($_POST["tgroupid"]))
{
	$_wheresql[] = "((tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."') OR (kbarticlelinks.kbcategoryid = 0))";
}

if (!empty($_POST["kbcategoryid"])) {
	$kbcategoryidlist = array();
	$kbcategoryidlist = fetchChildCategories($_POST["kbcategoryid"], ($_temp = array()));
	$kbcategoryidlist[] = $_POST["kbcategoryid"];
	$_wheresql[] = "(kbarticlelinks.kbcategoryid IN (". buildIN($kbcategoryidlist) ."))";
}

if (!empty($_POST["datefrom"]))
{
	$_datefrom = getCalendarDateline($_POST["datefrom"]);
	if (empty($_POST["dateto"]))
	{
		$_dateto = $_datefrom+86400;
	} else {
		$_dateto = getCalendarDateline($_POST["dateto"]);
	}

	$_wheresql[] = "(kbarticles.dateline > '". intval($_datefrom) ."' AND kbarticles.dateline < '". intval($_dateto) ."')";
}

$_articles = array();
$dbCore->query("SELECT kbarticles.*, kbarticlelinks.*, kbcategories.categorytype FROM `". TABLE_PREFIX ."kbarticles` AS kbarticles LEFT JOIN `". TABLE_PREFIX ."kbarticlelinks` AS kbarticlelinks ON (kbarticles.kbarticleid = kbarticlelinks.kbarticleid) LEFT JOIN `". TABLE_PREFIX ."kbcategories` AS kbcategories ON (kbarticlelinks.kbcategoryid = kbcategories.kbcategoryid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (kbarticlelinks.kbcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_KNOWLEDGEBASE ."')". iif(count($_wheresql), " WHERE ".implode(" AND ", $_wheresql)) ." ORDER BY kbarticles.views DESC LIMIT 20;");
while ($dbCore->nextRecord())
{
	$_articles[$dbCore->Record["kbarticleid"]] = $dbCore->Record;
}

renderKBReportGrid($_articles, "populararticles");

$template->assign("backurl", "index.php?_m=knowledgebase&_a=managecategories");

$interface->staffFooter();
?>